package com.qing.common.utils.json;

import cn.hutool.core.annotation.Alias;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.qing.common.utils.json.hutool.ConvertorEnum;
import com.qing.common.utils.json.hutool.HutoolJsonUtil;
import com.qing.common.utils.json.jackson.JacksonUtil;
import lombok.Data;

import java.io.IOException;

/**
 * @author guoqf
 */
public class JsonUtilTest {

    public static void main(String[] args) {
        TestJson testJson = new TestJson();
        testJson.setId("111");
//        testJson.setName("zhangsan");
        testJson.setMyAddress("adfasdfasdf");
        testHutool(testJson);

        testJackson(testJson);
    }

    private static void testHutool(TestJson json) {
        String s = HutoolJsonUtil.toJsonStr(json);
        System.out.println("============Hutool json test: " + s);
    }


    private static void testJackson(TestJson json) {
        String s = JacksonUtil.toJsonStr(json);
        System.out.println("============Jackson json test: " + s);

        try {
            String str = "{\"id\":\"111\",\"myAddress\":\"adfasdfasdf\",\"convertorEnum\":\"SmsKey\"}";
            TestJson testJson = JacksonUtil.stringToJson(str, TestJson.class);
            System.out.println("============Jackson json string to json: ");
            System.out.println(testJson);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    @Data
    public static class TestJson {
        private String id;

        @JsonSetter(nulls = Nulls.AS_EMPTY)
        private String name;

        @Alias("my_address")
        private String myAddress;

        @JsonSetter
        private ConvertorEnum convertorEnum;
    }
}
